package com.wind.sqlite.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wind.sqlite.common.base.BaseModelPageDTO;
import com.wind.sqlite.common.utils.PageUtils;
import com.wind.sqlite.common.utils.Query;
import com.wind.sqlite.dao.ColumnWhiteListTDao;
import com.wind.sqlite.entity.ColumnWhiteListTEntity;
import com.wind.sqlite.service.ColumnWhiteListTService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;


@Service("columnWhiteListTService")
public class ColumnWhiteListTServiceImpl extends ServiceImpl<ColumnWhiteListTDao, ColumnWhiteListTEntity> implements ColumnWhiteListTService {

  @Autowired
  private ColumnWhiteListTDao columnWhiteListTDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<ColumnWhiteListTEntity> page = this.page(
                new Query<ColumnWhiteListTEntity>().getPage(params),
                new QueryWrapper<ColumnWhiteListTEntity>()
        );
        return new PageUtils(page);
    }

  @Override
  public PageUtils queryPage(BaseModelPageDTO<ColumnWhiteListTEntity> data) {
      ColumnWhiteListTEntity entity = data.getData();
    QueryWrapper<ColumnWhiteListTEntity> queryWrapper = null;
    //分页查询所有
    if(entity==null){
      queryWrapper = new QueryWrapper<ColumnWhiteListTEntity>();
    }else{
      queryWrapper = new QueryWrapper<ColumnWhiteListTEntity>();
      queryWrapper = getWhiteListTEntityQueryWrapper(entity, queryWrapper);
    }
    IPage<ColumnWhiteListTEntity> page = this.page(
        new Query<ColumnWhiteListTEntity>().getPage(data),
        queryWrapper
    );
    return new PageUtils(page);
  }

  @Override
  public PageUtils queryPage(ColumnWhiteListTEntity entity) {
    BaseModelPageDTO<ColumnWhiteListTEntity> data=new BaseModelPageDTO<ColumnWhiteListTEntity>();
    data.setData(entity);
    return queryPage(data);
  }

    @Override
    public int remove(ColumnWhiteListTEntity entity) {
        QueryWrapper<ColumnWhiteListTEntity> queryWrapper = null;
        //分页查询所有
        if(entity==null){
            queryWrapper = new QueryWrapper<ColumnWhiteListTEntity>();
        }else{
            queryWrapper = new QueryWrapper<ColumnWhiteListTEntity>();
            queryWrapper = getWhiteListTEntityQueryWrapper(entity, queryWrapper);
        }
        return columnWhiteListTDao.delete(queryWrapper);
    }

    @Override
    public List<ColumnWhiteListTEntity> query(ColumnWhiteListTEntity entity) {
        QueryWrapper<ColumnWhiteListTEntity> queryWrapper = null;
        //分页查询所有
        if(entity==null){
            queryWrapper = new QueryWrapper<ColumnWhiteListTEntity>();
        }else{
            queryWrapper = new QueryWrapper<ColumnWhiteListTEntity>();
            queryWrapper = getWhiteListTEntityQueryWrapper(entity, queryWrapper);
        }
        return columnWhiteListTDao.selectList(queryWrapper);
    }

    private QueryWrapper<ColumnWhiteListTEntity> getWhiteListTEntityQueryWrapper(ColumnWhiteListTEntity entity, QueryWrapper<ColumnWhiteListTEntity> queryWrapper) {

       if(StringUtils.isNotEmpty(entity.getIp())){
       queryWrapper=queryWrapper.eq("ip", entity.getIp());
       }
        if(entity.getCreateTimeBeginTime()!=null){
           queryWrapper=queryWrapper.ge("create_time", entity.getCreateTimeBeginTime());
           }
        if(entity.getCreateTimeEndTime()!=null){
           queryWrapper=queryWrapper.le("create_time", entity.getCreateTimeEndTime());
           }
        if(entity.getUpdateTimeBeginTime()!=null){
           queryWrapper=queryWrapper.ge("update_time", entity.getUpdateTimeBeginTime());
           }
        if(entity.getUpdateTimeEndTime()!=null){
           queryWrapper=queryWrapper.le("update_time", entity.getUpdateTimeEndTime());
           }
    return queryWrapper;
  }


}